﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TTTExtended.Data;
using TTTExtended.Services.Models;

namespace TTTExtended.Services.Controllers
{
    public class GamesController : BaseApiController
    {
        // GET api/games
        public IQueryable<ScoreRecordModel> Get()
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new dbd70884f86766442b9b57a24d00ecbe4fEntities();

                var players = context.Players.
                    OrderByDescending(x => x.Wins.Count).ThenByDescending(x => x.Ties.Count).ThenBy(x => x.Losses.Count).Take(10);
                if (players == null)
                {
                    throw new InvalidOperationException(
                        "No Players in database");
                }
                var topPlayers =
                    (from player in players
                     select new ScoreRecordModel()
                     {
                          Losses = player.Losses.Count,
                          Wins = player.Wins.Count,
                          Ties = player.Ties.Count,
                          PlayerName = player.Name
                     });


                return topPlayers;
            });

            return responseMsg;
        }

        // POST api/games
        public HttpResponseMessage Post([FromBody]GameModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new dbd70884f86766442b9b57a24d00ecbe4fEntities();
                    using (context)
                    {
                        var player = context.Players.FirstOrDefault(
                            p => p.Name == model.PlayerName);

                        if (player == null)
                        {
                            player = new Player();
                            player.Name = model.PlayerName;

                            context.Players.Add(player);
                            context.SaveChanges();
                        }

                        var opponent = context.Players.FirstOrDefault(
                            p => p.Name == model.Opponent);

                        if (opponent == null)
                        {
                            opponent = new Player();
                            opponent.Name = model.Opponent;

                            context.Players.Add(opponent);
                            context.SaveChanges();
                        }

                        if (model.Status == "tie")
                        {
                            context.Ties.Add(new Tie()
                            {
                                 Player = player,
                                 Player1 = opponent
                            });

                            opponent.Ties.Add(new Tie()
                            {
                                Player = opponent,
                                Player1 = player
                            });
                            context.SaveChanges();
                        }
                        else if (model.Status == "win")
                        {
                            context.Wins.Add(new Win()
                            {
                                Player = player,
                                Player1 = opponent
                            });

                            context.Losses.Add(new Loss()
                            {
                                Player = opponent,
                                Player1 = player
                            });
                            context.SaveChanges();
                        }
                        else if (model.Status == "loss")
                        {
                            context.Wins.Add(new Win()
                            {
                                Player = player,
                                Player1 = opponent
                            });

                            context.Losses.Add(new Loss()
                            {
                                Player = opponent,
                                Player1 = player
                            });
                            context.SaveChanges();
                        }

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.OK);

                        return response;
                    }
                });

            return responseMsg;
        }
    }
}
